<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue'
import { useRoute } from 'vue-router';
import { userGetMenu, userSetMenu, menuList } from '../../../api/index'
import {Plus} from '@element-plus/icons-vue'

// form表单 ref引用
const formRef = ref(null)
// 树形 ref引用
const treeRef = ref(null)
// 树形弹窗权限数据
const permissionData = ref([])
// 表单数据
const formData = reactive({
  name: '',
  permissions: '',
  id: '',
})
// 分页数据
const paginationData = reactive({
  pageNum: 1,
  pageSize: 10,
})
// 菜单列表
let tableData = reactive({
  list: [],
  total: 0,
})
// 弹窗显示隐藏
const dialogFormVisable = ref(false)
// 选中权限
const defaultKeys = [4, 5]
// 表单验证规则
const rules = reactive({
  name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
})

// 关闭弹窗
function beforeClose() {
  dialogFormVisable.value = false
  // 重置添加表达
  formRef.value.resetFields()
  // 重置tree 的选择
  treeRef.value.setCheckedKeys(defaultKeys)
}

// 打开弹窗
function open(rowData = {}) {
  dialogFormVisable.value = true
  // 弹窗打开form生成是异步的
  nextTick(() => {
    if (rowData) {
      Object.assign(formData, rowData)
      treeRef.value.setCheckedKeys(rowData.permission)
    }
  })
}

// 表单提交
const confirm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 获取到选择的复选框的数据
      const permissions = JSON.stringify(treeRef.value.getCheckedKeys())
      userSetMenu({
        name: formData.name,
        permissions,
        id: formData.id,
      }).then(({ data }) => {
        if (data.code === 10000) {
          ElMessage.success(data.data)
          // 关闭弹窗
          dialogFormVisable.value = false
          // 刷新数据
          getListData()
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

// 请求列表数据
async function getListData() {
  const {
    data: { data },
  } = await menuList(paginationData)
  tableData.list = data.list
  tableData.total = data.total
}

// 获取权限数据
onMounted(async () => {
  // 获取详细的权限数据
  const { data } = await userGetMenu()
  permissionData.value = data.data
  getListData()
})

// 切换页数
function handleSizeChange(val) {
  paginationData.pageSize = val
  getListData()
}
// 翻页
function handleCurrentChange(val) {
  paginationData.pageNum = val
  getListData()
}
</script>

<template>
  <!-- 面板 -->
  <PanelHead :routeData="useRoute()"></PanelHead>
  <div class="btns">
    <el-button type="primary" :icon="Plus" @click="open(null)">添加</el-button>
  </div>
  <el-table :data="tableData.list" style="width: 100%">
    <el-table-column prop="id" label="id"></el-table-column>
    <el-table-column prop="name" label="名称"></el-table-column>
    <el-table-column
      prop="permissionName"
      label="菜单权限"
      width="500px"
    ></el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="paginatin-info">
    <!-- 分页器 -->
    <el-pagination
      :page-size="paginationData.pageSize"
      :background="false"
      size="small"
      layout="total, prev, pager, next"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
  <!-- 弹窗表单 -->
  <el-dialog
    v-model="dialogFormVisable"
    :before-close="beforeClose"
    title="添加权限"
    width="500"
  >
    <el-form
      ref="formRef"
      label-width="100px"
      label-position="left"
      :model="formData"
      :rules="rules"
    >
      <el-form-item v-show="false" prop="id">
        <el-input v-model="formData.id"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入权限名称" />
      </el-form-item>
      <el-form-item label="权限" prop="permissions">
        <el-tree
          ref="treeRef"
          style="max-width: 600px"
          :data="permissionData"
          node-key="id"
          show-checkbox
          :defaultCheckedKeys="defaultKeys"
        >
        </el-tree>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.btns {
  background: #fff;
  padding: 10px;
}
.paginatin-info {
  background-color: #fff;
  height: 40px;
  .el-pagination {
    height: 100%;
    box-sizing: border-box;
    padding:0 10px;
  }
}

</style>
